<!--
 * @Author: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @Date: 2024-02-23 14:28:54
 * @LastEditors: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @LastEditTime: 2024-02-26 19:47:31
 * @FilePath: \hrManage\src\views\department\components\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="close">
    <el-form
      ref="deptForm"
      label-width="120px"
      :model="deptValue"
      :rules="deptRules"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="deptValue.name"
          placeholder="请输入2-10个字符"
          style="width: 80%"
          size="mini"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="deptValue.code"
          placeholder="请输入2-10个字符"
          style="width: 80%"
          size="mini"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select
          v-model="deptValue.managerId"
          placeholder="请选择负责人"
          style="width: 80%"
          size="mini"
        >
          <el-option
            v-for="item in manager"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门简介" prop="introduce">
        <el-input
          v-model="deptValue.introduce"
          type="textarea"
          placeholder="1-100字符"
          style="width: 80%"
          size="mini"
        />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button
              size="mini"
              type="primary"
              @click="btnOk"
            >提交</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {
  getDepartment,
  getDepartmentManager,
  getDepartmentDetail,
  addDepartment,
  updateDepartment
} from '@/api/department'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    childId: {
      type: Number,
      default: undefined
    }
  },
  data() {
    return {
      manager: [],
      deptValue: {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      },
      deptRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度2-10个字符', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: async(rules, value, callback) => {
              let res = await getDepartment()
              if (this.deptValue.id) {
                res = res.filter((item) => item.id !== this.deptValue.id)
              }
              if (
                res.some((item) => {
                  return item.name === value
                })
              ) {
                callback(new Error('部门已存在'))
              } else {
                callback()
              }
            }
          }
        ],
        code: [
          { required: true, message: '请输入部门编号', trigger: 'blur' },
          { min: 2, max: 10, message: '长度2-10个字符', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: async(rules, value, callback) => {
              let res = await getDepartment()
              if (this.deptValue.id) {
                res = res.filter((item) => item.id !== this.deptValue.id)
              }
              if (
                res.some((item) => {
                  return item.code === value
                })
              ) {
                callback(new Error('部门已存在'))
              } else {
                callback()
              }
            }
          }
        ],
        managerId: '',
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 100, message: '长度1-100个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.deptValue.id ? '修改部门' : '添加部门'
    }
  },
  created() {
    this.getDepartmentManager()
  },
  methods: {
    close() {
      this.deptValue = {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      }
      this.$refs.deptForm.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getDepartmentManager() {
      const res = await getDepartmentManager()
      this.manager = res
      console.log(res)
    },
    btnOk() {
      this.$refs.deptForm.validate(async(isOk) => {
        if (isOk) {
          let msg = '添加'
          if (this.deptValue) {
            await updateDepartment(this.deptValue)
            msg = '修改'
          } else {
            await addDepartment({ ...this.deptValue, pid: this.childId })
          }
          this.$message.success(`${msg}成功`)
          this.$emit('Department')
          this.close()
        }
      })
    },
    async update() {
      this.deptValue = await getDepartmentDetail(this.childId)
    }
  }
}
</script>

<style>
</style>
